﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_WECHAT_GAME
using WeChatWASM;
#endif

namespace HBFramework
{
    public class SafeAreaUtility
    {
        /// <summary>
        /// 转换Rect
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static void SafeRect(RectTransform rectTran)
        {
            float offsetMinX, offsetMinY = 0;
            float offsetMaxX, offsetMaxY = 0;
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
            var info = WX.GetWindowInfo();
            offsetMinX = (float)(info.safeArea.left * info.pixelRatio);
            offsetMinY = (float)((info.windowHeight - info.safeArea.bottom) * info.pixelRatio);
            offsetMaxX = (float)((info.safeArea.right - info.windowWidth) * info.pixelRatio);
            offsetMaxY = (float)(-info.safeArea.top * info.pixelRatio);
#else
            offsetMinX = Screen.safeArea.min.x;
            offsetMinY = Screen.safeArea.min.y;
            offsetMaxX = Screen.safeArea.max.x - Screen.width;
            offsetMaxY = Screen.safeArea.max.y - Screen.height;
#endif
            Debug.Log($"{offsetMinX}:{offsetMinY}:{offsetMaxX}:{offsetMaxY}");
            rectTran.offsetMin += new Vector2(offsetMinX, offsetMinY);
            rectTran.offsetMax += new Vector2(offsetMaxX, offsetMaxY);
        }

        /// <summary>
        /// 获取顶部偏移
        /// </summary>
        /// <returns></returns>
        public static float GetTopOffset()
        {
            float result = 0;
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
            var info = WX.GetWindowInfo();
            result = (float)(-info.safeArea.top * info.pixelRatio);
#else
            result = Screen.safeArea.max.y - Screen.height;
#endif
            return result;
        }
    }
}